<div class="member-main">
<h1 class="title"><{t}>申请售后服务<{/t}></h1>
<form action="<{link app=aftersales ctl=site_member act=return_save}>" enctype="multipart/form-data" method="post" name="return_save" id='x-return-form'>
<div class="FormWrap">
    <div class="division">
    <h4 class="fontnormal"><{t}>请输入售后服务标题<{/t}></h4>
    <{input name="title" id="x-return-title" class='x-inputs' type="text" size="60"}>
	<input type="hidden" vtype="title_required&&title_weight" />
  </div>
  <div class="division">
    <h4 class="fontnormal"><{t}>请勾选需要售后服务的商品及数量<{/t}></h4>

   <div id="x-return-item-list">
	<{include file="site/member/return_list_item.html" app=aftersales}>
	<input type="hidden" vtype="product_nums_required" />
	</div>

    <div class="p5">
      <ol id="x-return-item-ol">
      </ol>
    </div>
  </div>
  <div class="division">
	<h4 class="fontnormal"><{t}>如果您有相关附件，可以在此提交，格式:jpg,gif,jpeg，文件不宜过大，可能会上传失败<{/t}></h4>
	<input type="hidden" name="MAX_FILE_SIZE" value="8300000" />
    <input type="file" class="x-input x-inputs" name="file" id="fileField" />
    </div>
  </div>
  <div class="division" style="border-top:none">
    <h4 class="fontnormal"><{t}>申请售后的详细原因描述<{/t}></h4>
    <{input type="textarea" id="x-return-content" name="content" class='x-inputs x-input' cols="80" rows="5" style="width:98%"}>
	<input type="hidden" vtype="return_content_required&&return_content_weight" />
	 <div class="textcenter p10">
    	<button class="btn order-btn" type="submit"><span><span><{t}>提交申请<{/t}></span></span></button>
  </div>
  </div>

</div>
<input type="hidden" name="order_id" value="<{$order_id}>">
</form>
</div>
<script>
function init_returnlist(){
	$$('#x-return-item-list input[type="checkbox"]').addEvent('click',function(e){
		e = $(e.target);
        var parent = e.getParent('tr');
		if(e.checked){
			var s = '{0} <input type="hidden" name="products[]" value="{1}">x <{input type="text" product_nums="{2}" class="x-product-nums" name="product_nums[{1}]" value="{3}" size="2" }><{t}> 件<{/t}><button onclick="del_return_item(this,'+e.value+')" type="button"><{t}>删除<{/t}></button><input type="hidden" name="product_name[{1}]" value="{4}"><input type="hidden" name="product_bn[{1}]" value="{5}"><input type="hidden" vtype="rtn_digits&&nominus&&product_nums" />';
            var el = new Element('li',{id:'x-item-'+e.value}).set('html', s.format(parent.getElement('.x-pdt-name').innerHTML,e.value,parent.getElement('.x-pdt-num').innerHTML,parent.getElement('.x-pdt-num').innerHTML,parent.getElement('.x-pdt-name').innerHTML,parent.getElement('.x-pdt-bn').innerHTML)).inject($('x-return-item-ol'));
			$$('#x-return-form .x-input[name^=product_nums]').addEvent('mouseover',function(e){
				var _li = this.getParent('li');
				if (_li){
					var error_el = _li.getElement('.x-product-nums-error');
					if(error_el){
						error_el.destroy();
					}
				}
			});
			var error_el = $('x-return-item-list-error');
			if( error_el )
			{
				error_el.destroy();
			}
		}else{
           var el= $('x-item-'+e.value);
           if(el){
               el.destroy();
           }
		}
	});

}

function jump_to_return_list(page){
    new Request.HTML({
		url:'<{$url}>',
		method:'post',
		update:$('x-return-item-list'),data:'page='+page,
		onSuccess:function(){
			init_returnlist();
			var el = $('x-return-item-list');
			var chk_arr = el.getElements('input[type=checkbox]');
			chk_arr.each(function(item,index){
				var elol = $('x-return-item-ol');
				var inol_arr = elol.getElements('input[type=hidden]');
				inol_arr.each(function(itemol,indexol){
					if(item.value == itemol.value){
						item.checked = true;
					}
				});
			})
		}}).send();
}

function del_return_item(e,product_id){
    var el = $(e).getParent('li');
    var product_list = $("x-return-item-list");
	var elchk = product_list.getElement('.x-pdt-chk-'+product_id);
	if( elchk ){
		var checked = elchk.checked;
		if( checked )
		{
		   elchk.checked = false;
		}
	}
    el.destroy();

	return false;
}
init_returnlist();


$$('#x-return-form .x-inputs').addEvent('mouseover',function(e){
	e = e.target;
	var id = e.get('id');
	var error_el = $(id+'-error');
	if( error_el )
	{
		error_el.destroy();
	}
});

// 退货数量的验证
validatorMap['product_nums'] = ['<{t}>申请售后商品的数量不能大于订购数量<{/t}>', function(element, v) {
	var item = $(element).getParent('li').getElement('input[name^=product_nums]');

	if 	(parseFloat(item.value) > parseFloat(item.get('product_nums')))
		return false;
	else
		return true;
}];

validatorMap['nominus'] = ['<{t}>退货数量不能小于0<{/t}>', function(element, v) {
	var item = $(element).getParent('li').getElement('input[name^=product_nums]');

	if 	(item.value <= 0)
		return false;
	else
		return true;
}];

validatorMap['rtn_digits'] = ['<{t}>请输入整数<{/t}>', function(element, v) {
	var item = $(element).getParent('li').getElement('input[name^=product_nums]');

	return !/[^\d]/.test(item.value);
}];

validatorMap['product_nums_required'] = ['<{t}>您没有选择商品，请先选择商品。<{/t}>', function(element, v) {
	var item = $(element).getParent('div').getParent('div').getElement('#x-return-item-ol').getElements('input[name^=product_nums]');

	return item.length > 0;
}];

validatorMap['title_required'] = ['<{t}>请填写售后服务标题<{/t}>', function(element, v) {
	var item = $(element).getParent('div').getElement('input[name^=title]');

	return item.value.length != 0;
}];

validatorMap['title_weight'] = ['<{t}>您的填写有误，标题2—20字数之内<{/t}>', function(element, v) {
	var item = $(element).getParent('div').getElement('input[name^=title]');

	return item.value.length >= 2 && item.value.length <= 30;
}];

validatorMap['return_content_required'] = ['<{t}>请填写售后服务详细原因<{/t}>', function(element, v) {
	var item = $(element).getParent('div').getElement('#x-return-content');

	return item.value.length != 0;
}];

validatorMap['return_content_weight'] = ['<{t}>您的填写有误，详细原因应不小于10个字数<{/t}>', function(element, v) {
	var item = $(element).getParent('div').getElement('#x-return-content');

	return item.value.length >= 10;
}];
/** end **/

</script>
